using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using FreeSilverlightChart;

namespace CandleStickChartUsage
{
  public partial class Page : UserControl
  {
    public Page()
    {
      InitializeComponent();
      this.Loaded += new RoutedEventHandler(Page_Loaded);
    }

    public void Page_Loaded(object o, EventArgs e)
    {
      _hdoc = HtmlPage.Document;

      HtmlElement element = _hdoc.GetElementById("switchSeries");
      element.AttachEvent("onclick",
          new EventHandler<HtmlEventArgs>(this.MultipleSeries));

      LoadChart(null, null);
    }

    public void MultipleSeries(object sender, HtmlEventArgs args)
    {
      _switchSeries = !_switchSeries;
      LoadChart(sender, args);
    }

    public void LoadChart(object sender, HtmlEventArgs args)
    {
      if(_chart != null)
      {
        parentCanvas.Children.Remove(_chart);
      }

      //the default type of the chart is vertical bar-chart.
      Chart.ChartType chartType = Chart.ChartType.CANDLE_STICK;

      //set all the information of the chart.
      ChartModel model = _loadChartModel();

      // create the chart 
      Chart chart = Chart.CreateChart(chartType, model);

      //There is no 3d for candlestick
      chart.IsPerspective = false;

      // Since we have large number of labels lets rotate them
      chart.RotateHorizontalLabels = true;

      //set the position of the series labels
      chart.LegendPosition = Chart.LegendLocation.BOTTOM;

      //set the duration of the animation
      chart.AnimationDuration = 2.0;

      //set the formate of the 
      chart.Format = "$#,##0.00;($#,##0.00);0";

      //set the size of the chart area.
      chart.SetBounds(new Rect(0, 0, parentCanvas.Width, parentCanvas.Height));
      // add the custom component
      parentCanvas.Children.Add(chart);

      // setup the event handler so that when mouse enter show show the information of the bar. 
      chart.ChartClicked += new ChartEventHandler(ChartClicked);
      
      _chart = chart;
      
      // now draw it
      chart.Draw();
    }

    private void ChartClicked(object sender, ChartEventArgs e)
    {
      CandleStickChartModel model = _chart.Model as CandleStickChartModel;

      var j = e.SeriesIndices[0];
      var i = e.YValueIndices[0];

      double[][][] candleStickYValues = model.CandleStickYValues;

      //top label
      //Show the open-high-low-close values as comma separated
      StringBuilder sb = new StringBuilder();
      sb.Append(candleStickYValues[i][j][0].ToString(_chart.Format));
      sb.Append(", ");
      sb.Append(candleStickYValues[i][j][1].ToString(_chart.Format));
      sb.Append(", ");
      sb.Append(candleStickYValues[i][j][2].ToString(_chart.Format));
      sb.Append(", ");
      sb.Append(candleStickYValues[i][j][3].ToString(_chart.Format));
      alertText.Text = sb.ToString();

      RectangleGeometry rg = alertRect.Data as RectangleGeometry;
      Rect rect = rg.Rect;
      double left = Math.Max(5.0, (rect.Width - alertText.ActualWidth) / 2);
      double top = Math.Max(5.0, (rect.Height - 20 - alertText.ActualHeight) / 2);
      alertText.SetValue(Canvas.LeftProperty, left);
      alertText.SetValue(Canvas.TopProperty, top);
      alert.Visibility = Visibility.Visible;
    }

    protected void ButtonMouseEnter(object sender, MouseEventArgs e)
    {
      Path path = okRect;
      LinearGradientBrush lgb = path.Fill as LinearGradientBrush;
      lgb.GradientStops[0].Color = Color.FromArgb(255, 240, 188, 77);
    }

    protected void ButtonMouseLeave(object sender, EventArgs e)
    {
      Path path = okRect;
      LinearGradientBrush lgb = path.Fill as LinearGradientBrush;
      lgb.GradientStops[0].Color = Color.FromArgb(255, 180, 196, 139);
    }

    protected void OKClick(object sender, MouseEventArgs e)
    {
      alert.Visibility = Visibility.Collapsed;
    }


    // load the data of the chart here.
    private ChartModel _loadChartModel()
    {
      // the name of the each group
      string[] _groupLabels = { "1-Nov-05", "8-Nov-05", "15-Nov-05", "22-Nov-05", 
                                "29-Nov-05", "06-Dec-05", "13-Dec-05", "20-Dec-05",
                                "27-Dec-05", "03-Jan-08", "10-Jan-08","17-Jan-08"};
      // the name of each series.
      string[] _seriesLabels = { "Stock1"};

      // data from 1-Nov-05 through 16-Jan-06
      double[][][] chartYValues = _switchSeries ? _getOtherSeriesData() : _getData();
      string[] _seriesColors = { "#E76D48", "#6EA6F3", "#9DCE6E", "#FCC46F", "#FF7FFF", "#AEAEAE" };


      Color[] colors = new Color[_seriesColors.Length];
      for (var i = 0; i < _seriesColors.Length; ++i)
      {
        byte r = byte.Parse(_seriesColors[i].Substring(1, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
        byte g = byte.Parse(_seriesColors[i].Substring(3, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
        byte b = byte.Parse(_seriesColors[i].Substring(5, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
        colors[i] = Color.FromArgb(255, r, g, b);
      }

      double minYValue = _switchSeries?65:136;
      double maxYValue = _switchSeries?91:145;

      string title = "Chart Data for Nov 2005 - Jan 2006";
      string subTitle = "This is sub-title";
      string footNote = "This is footnote";

      CandleStickChartModel model = new CandleStickChartModel(_seriesLabels, _groupLabels, chartYValues, title, subTitle, footNote, colors);

      model.MaxYValue = maxYValue;
      model.MinYValue = minYValue;

      model.SeriesColors = colors;
      return model;
    }

    private double [][][] _getData()
    {
      // data from 1-Nov-05 through 16-Jan-06
      return new double[][][]{
        new double[][]{new double[]{139.58,	140.54,	139.48,	140.41}},
        new double[][]{new double[]{140.37,	141.19,	140.18,	140.95}},
        new double[][]{new double[]{140.94,	141.17,	139.83,	140.09}},
        new double[][]{new double[]{140.10,	140.84,	139.41,	139.78}},
        null,
        null,
        new double[][]{new double[]{139.61, 139.63, 138.50, 138.68}},
        new double[][]{new double[]{138.68,	138.72,	137.80,	138.14}},
        new double[][]{new double[]{138.13,	138.71,	137.61,	138.68}},
        new double[][]{new double[]{138.66,	138.77,	137.72,	137.76}},
        new double[][]{new double[]{137.75,	138.61,	137.50,	138.61}},
        null,
        null,
        new double[][]{new double[]{138.38,	139.37,	138.30,	138.89}},
        new double[][]{new double[]{138.89,	139.50,	138.84,	139.45}},
        new double[][]{new double[]{139.40,	139.86,	138.90,	139.10}},
        new double[][]{new double[]{139.07,	139.71,	138.50,	139.58}},
        new double[][]{new double[]{139.57,	140.32,	139.07,	140.23}},
        null,
        null,
        new double[][]{new double[]{140.33,	140.74,	139.30,	139.77}},
        new double[][]{new double[]{139.73,	140.47,	139.47,	140.42}},
        new double[][]{new double[]{140.41,	140.45,	139.54,	140.23}},
        new double[][]{new double[]{140.21,	140.42,	139.92,	140.30}},
        new double[][]{new double[]{140.31,	140.64,	140.11,	140.25}},
        null,
        null,
        new double[][]{new double[]{140.24,	140.96,	139.99,	140.87}},
        new double[][]{new double[]{140.89,	141.10,	140.41,	140.90}},
        new double[][]{new double[]{140.92,	141.37,	140.52,	141.23}},
        new double[][]{new double[]{141.30,	141.63,	140.78,	141.35}},
        new double[][]{new double[]{141.40,	141.77,	140.56,	141.18}},
        null,
        null,
        new double[][]{new double[]{141.85,	142.71,	141.66,	142.45}},        
        new double[][]{new double[]{142.44,	142.81,	142.18,	142.36}},
        new double[][]{new double[]{142.36,	142.58,	141.56,	141.81}},
        new double[][]{new double[]{141.83,	142.60,	141.31,	142.30}},
        new double[][]{new double[]{142.29,	142.68,	141.77,	142.52}},
        null,
        null,
        new double[][]{new double[]{142.51,	143.62,	142.44,	143.34}},
        new double[][]{new double[]{143.36,	143.60,	143.11,	143.47}},
        new double[][]{new double[]{143.47,	143.51,	140.17,	140.75}},
        new double[][]{new double[]{140.76,	140.99,	138.98,	139.21}},
        new double[][]{new double[]{139.19,	139.96,	138.21,	138.94}},
        null,
        null,
        new double[][]{new double[]{139.12,	140.06,	138.90,	139.34}},
        new double[][]{new double[]{139.35,	139.95,	138.55,	139.28}},
        new double[][]{new double[]{139.28,	139.41,	138.55,	139.01}},
        new double[][]{new double[]{139.01,	139.24,	138.29,	138.49}},
        new double[][]{new double[]{138.49,	138.65,	137.59,	137.96}},
        null,
        null,
        new double[][]{new double[]{137.97,	138.56,	137.59,	137.97}},
        new double[][]{new double[]{137.99,	139.16,	137.94,	139.11}},
        new double[][]{new double[]{139.10,	139.93,	138.82,	139.48}},
        new double[][]{new double[]{139.48,	139.89,	139.31,	139.64}},
        new double[][]{new double[]{139.62,	139.84,	138.71,	139.69}},
        null,
        null,
        new double[][]{new double[]{139.65,	139.74,	139.03,	139.13}},
        new double[][]{new double[]{139.19,	139.86,	138.92,	139.53}},
        new double[][]{new double[]{139.52,	140.89,	139.42,	140.80}},
        new double[][]{new double[]{140.79,	140.91,	140.00,	140.41}},
        new double[][]{new double[]{140.43,	140.64,	138.88,	139.04}},
        null,
        null,
        new double[][]{new double[]{139.03,	139.06,	137.67,	138.30}},
        new double[][]{new double[]{138.30,	138.49,	137.80,	138.05}},
        new double[][]{new double[]{138.07,	138.77,	137.93,	138.76}},
        new double[][]{new double[]{138.73,	138.81,	137.11,	138.00}},
        new double[][]{new double[]{138.02,	138.83,	137.85,	138.53}},
        null,
        null,
        new double[][]{new double[]{138.54,	139.01,	138.46,	138.98}},
        };
    }

    private double[][][] _getOtherSeriesData()
    {
      
      // data from 1-Nov-05 through 16-Jan-06
      return new double[][][]{
        new double[][]{new double[]{71.40,	72.25,	71.40000153,	 71.75}},
        new double[][]{new double[]{73.000,	73.48000336,	71.02999878,	 71.30 }},
        new double[][]{new double[]{72.450,	73.90000153,	72,	 73.50 }},
        new double[][]{new double[]{67.950,	70.70999908,	67.87000275,	 70.51 }},
        null,
        null,
        new double[][]{new double[]{67.850,	69.40000153,	67.69999695,	 68.58 }},
        new double[][]{new double[]{72.010,	72.41000366,	67.16000366,	 67.60 }},
        new double[][]{new double[]{71.350,	73.62000275,	71.25,	 72.00 }},
        new double[][]{new double[]{71.200,	72.15000153,	69.09999847,	 71.90 }},
        new double[][]{new double[]{66.750,	70.58999634,	66.09999847,	 70.05 }},
        null,
        null,
        new double[][]{new double[]{70.300,	71.44999695,	68.41000366,	 68.60 }},
        new double[][]{new double[]{67.250,	71.48999786,67.25,	 69.70 }},
        new double[][]{new double[]{70.300,	70.98999786,	68.19000244,	 68.75 }},
        new double[][]{new double[]{74.000,	74.08999634,	71.43000031	, 71.58 }},
        new double[][]{new double[]{74.200,	75.19999695,	73.25,	 73.35 }},
        null,
        null,
        new double[][]{new double[]{75.600,	76.90000153,	75.47000122	, 75.94 }},
        new double[][]{new double[]{76.180,	77.75,	75.16000366,	 77.14 }},
        new double[][]{new double[]{74.200,	76.76999664,	73.25,	 76.17 }},
        new double[][]{new double[]{75.450,	77.40000153,	75.30000305,	 75.60 }},
        new double[][]{new double[]{75.400,	75.94999695,	73.25,	 74.65 }},
        null,
        null,
        new double[][]{new double[]{77.850,	78.25,	75.19999695,	 75.49 }},
        new double[][]{new double[]{77.750,	78.80000305,	76.59999847	, 76.80 }},
        new double[][]{new double[]{78.480,	78.84999847,	77,	 78.30 }},
        new double[][]{new double[]{80.550,	80.94999695,	78.88999939,	 79.66 }},
        new double[][]{new double[]{79.650,	80.70999908,	79.11000061,	 80.55 }},
        null,
        null,
        new double[][]{new double[]{78.110,	79.65000153,	77.59999847	, 79.31 }},        
        new double[][]{new double[]{80.750,	80.91000366,	78,	 78.11 }},
        new double[][]{new double[]{82.050,	82.05000305,	80.40000153,	 80.45 }},
        new double[][]{new double[]{80.600,	82.25,	80.22000122,	 82.25 }},
        new double[][]{new double[]{81.500,	82,	81,	 81.60 }},
        null,
        null,
        new double[][]{new double[]{82.800,	83.11000061,	81.69000244,	 82.08 }},
        new double[][]{new double[]{83.650,	83.75,	82.62000275,	 83.10 }},
        new double[][]{new double[]{84.250,	85.05000305,	82.51999664,	 84.35 }},
        new double[][]{new double[]{83.760,	84.38999939,	82.73999786,	 84.00 }},
        new double[][]{new double[]{84.410,	85,	83.30000305,	 83.45 }},
        null,
        null,
        new double[][]{new double[]{85.450,	85.45999908,	83.93000031,	 84.45 }},
        new double[][]{new double[]{86.250,	86.40000153,	85,	 85.69 }},
        new double[][]{new double[]{84.200,	86,	83.40000153,	 85.45 }},
        new double[][]{new double[]{84.600,	86.48999786,	84.05000305,	 84.50 }},
        new double[][]{new double[]{84.500,	85.48000336,	82.19000244,	 85.48 }},
        null,
        null,
        new double[][]{new double[]{80.250,	82.29000092,	79.5,	 82.19 }},
        new double[][]{new double[]{79.950,	80.5,	79.25,	 79.68 }},
        new double[][]{new double[]{81.900,	83,	79,	 79.93 }},
        new double[][]{new double[]{78.500,	82.80000305,	78.19999695,	 82.45 }},
        new double[][]{new double[]{76.890,	77.5,	75.91999817,	 76.50 }},
        null,
        null,
        new double[][]{new double[]{81.700,	81.87000275,	76,	 76.00 }},
        new double[][]{new double[]{83.100,	83.5,	81.11000061,	 81.78 }},
        new double[][]{new double[]{83.750,	84.80000305,	83.12999725,	 83.86 }},
        new double[][]{new double[]{83.850,	84.65000153,	81.66999817,	 84.30 }},
        new double[][]{new double[]{83.910,	85.23000336,	83.52999878,	 83.76 }},
        null,
        null,
        new double[][]{new double[]{86.700,	86.95999908,	84.69999695,	 84.71 }},
        new double[][]{new double[]{86.050,	86.91999817,	85.76999664,	 86.71 }},
        new double[][]{new double[]{88.200,	88.25,	86.26999664,	 86.50}},
        new double[][]{new double[]{88.350,	89.08000183,	87.23999786,	 87.33 }},
        new double[][]{new double[]{88.950,	88.94999695,	87.36000061,	 87.93 }},
        null,
        null,
        new double[][]{new double[]{89.900,	90.02999878,	87.59999847,	 89.00 }},
        };
    }

    private HtmlDocument _hdoc;
    private Chart _chart;
    private bool _switchSeries = false;
  }
}

